package com.hailiao.beidou.message.bd.ble

import com.hailiao.beidou.comm.bluetooth.BLEHub
import com.hailiao.beidou.comm.bluetooth.constant.BLEStatus
import com.hailiao.beidou.comm.bluetooth.data.NotifySettingInfo
import com.hailiao.beidou.core.BDCore
import com.hailiao.beidou.message.bd.Beidou
import com.hailiao.beidou.message.bd.constant.ProtoVersion
import com.hailiao.beidou.message.bd.data.BDDeviceStatusData
import com.hailiao.beidou.message.bd.data.toBDDeviceStatusDataList
import com.hailiao.beidou.privatehl.BDHDX_Data
import com.hailiao.beidou.privatehl.CCHDA_Data
import com.hailiao.beidou.privatehl.CCIDS_Data
import com.hailiao.beidou.privatehl.sendCCHDA
import com.hailiao.beidou.privatehl.sendCCIDS
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch

/**
 * 海聊北二设备
 * @Author d10ng
 * @Date 2024/3/5 10:21
 */
abstract class IRIMDevice : IBLEDevice {

    // 定时自检任务
    private var checkSelfJob: Job? = null

    // 自检数据
    private val checkSelfDataFlow = MutableStateFlow<BDHDX_Data?>(null)

    init {
        val scope = CoroutineScope(Dispatchers.IO)
        // 监听蓝牙连接状态
        scope.launch {
            BLEHub.statusFlow.filter { it == BLEStatus.DISCONNECT }.collect {
                checkSelfJob?.cancel()
            }
        }
        // 监听设备状态
        scope.launch {
            BDCore.receiveProtoSharedFlow.filter { it.protocol::class == BDHDX_Data::class }
                .map { it.protocol }.map { it as BDHDX_Data }.collect {
                    checkSelfDataFlow.value = it
                }
        }
    }

    override fun instantOf(name: String): Boolean {
        return false
    }

    override fun getIconID(): Int {
        return 0
    }

    override fun getProtoVersion(): ProtoVersion {
        return ProtoVersion.BD2
    }

    override fun getConnectSetting(): NotifySettingInfo {
        return NotifySettingInfo()
    }

    override fun getSendDataPackageSize(): Int {
        return 20
    }

    override suspend fun onDeviceConnected() {
        // 开启定时自检任务
        checkSelfJob = CoroutineScope(Dispatchers.IO).launch {
            launch {
                while (true) {
                    BLEHub.sendCCHDA(CCHDA_Data())
                    delay(1000 * 5)
                }
            }
        }
    }

    override suspend fun setCenterCard(card: String) {
        BLEHub.sendCCIDS(CCIDS_Data("1", card))
    }

    override fun getDeviceStatusDataListFlow(): Flow<List<BDDeviceStatusData>> {
        return checkSelfDataFlow.combine(Beidou.cardFlow) { self, card ->
            self.toBDDeviceStatusDataList() + card.toBDDeviceStatusDataList()
        }
    }

    private fun BDHDX_Data?.toBDDeviceStatusDataList() = listOf(
        BDDeviceStatusData(
            BDDeviceStatusData.Type.BATTERY,
            "设备电量",
            if (this == null) "--" else "${battery}%(${powerStatus.text})"
        )
    )
}
